In [1]:
print("Imagine all the people living life in peace... John Lennon")
To print part of a text on a new line, \n tag is used.
In [2]:
print("Imagine all the people \nliving life in peace... \nJohn Lennon")
To tabulate part of a text forward, \t tag is used.
In [3]:
print("Imagine all the people \nliving life in peace... \n\tJohn Lennon")
In [4]:
collection = raw_input("Input some numbers seprated by a comma: ")
In [5]:
print(collection)
In [6]:
type(collection)
Out[6]:
To convert the comma-separated string (as the one above, inputted by the user) into a list of elements, split() function is used.
In [7]:
# split function takes one argument: the character to split the string on (comma in our case).
our_list = collection.split(",")
In [8]:
print(our_list)
In [9]:
def adjectivizer(noun):
return noun+"ly"
In [10]:
adjectivizer("man")
Out[10]:
THe function below is the upgrade of above function. It first checks the length of the function argument. If it is more than 3, it adjectivizes the noun, else it adds "super" in front.
In [11]:
def superly(noun):
if len(noun)>3:
return noun+"ly"
else:
return "super"+noun
In [12]:
superly("cat")
Out[12]:
The function below is the upgrade of above function. It checks one more condition: adjectivizes if more than 4 letters, leaves the same in case of four (note the double equal sign) and adds the "super" in front in other cases.
In [13]:
def superly4(noun):
if len(noun)>4:
return noun+"ly"
elif len(noun)==4:
return noun
else:
return "super"+noun
In [14]:
superly4("late")
Out[14]:
Same function as above, just with one more condition checked (==5).
In [15]:
def superly5(noun):
if len(noun)>4:
return noun+"ly"
elif len(noun)==4:
return noun
elif len(noun)==5:
return "super"+noun
else:
return "to short noun"
In [16]:
superly5("truth")
Out[16]:
The function below asks the user to input some currency (\$ or e sign followd by amount of money) and converts it to Armenian drams. Those are the steps taken:
Then the result is printed.
In [17]:
amount = raw_input("Please, input currency followed by amount ")
def c_converter(amount):
if amount[0]=="$":
return str(484*int(amount[1:]))+" AMD"
elif amount[0]=="e":
return str(535*int(amount[1:])) + " AMD"
else:
return "Please, use $ or e sign in front"
print c_converter(amount)
Given the numbers between 7 (included) and 1700 (not included), let's choose those that can be divided by 5. For that reason the % (knows as "modulus") operator is used. % operator results in the residual after the division. For example, 5%2 will result in 1 and 6%4 will result in 2. In order to check whether some number can be divided by 5 or not, we must check whether numer%5 is 0 or not.
If we want to save the resulting values somewhere, then let's first create an empty list, which will then be added by 5-divisable numbers.
In [18]:
# create an empty list
our_list=[]
# iterate over all numbers in the given range
for number in range(7,1700):
# if the reminder is 0
if number%5==0:
# append/add that number to our list
our_list.append(number)
In [19]:
# let's print our list
print our_list
Above, are all the numbers that can be divided by 5 without residual. Let's now choose a smaller set of numbers. Let's choose those that can both be divided by 5 and by 7 without residual.
In [20]:
our_list=[]
for number in range(7,1700):
if number%5==0 and number%7==0:
our_list.append(number)
In [21]:
print our_list
As you nticed above, in order to add one more required condition inside if statement, one needs to use just and in between. Similarly, or can be used to checked whether at least one of the conditions is satisfied or not.
In [22]:
our_list=[]
for number in range(7,1700):
if number%5==0 or number%7==0:
our_list.append(number)
Let's define a function that will take two numbers as an argument and return the maximum of them
In [23]:
def max_of_2(x,y):
if x>y:
return x
else:
return y
In [24]:
print max_of_2(15,12)
Let's now define a max_of_3 function. As we already have max_of_2, we just need to nest them inside one another as shown below:
In [25]:
def max_of_3(x,y,z):
return max_of_2(z,max_of_2(x,y))
In [26]:
max_of_3(10,15,33)
Out[26]:
In [27]:
def reverser(text):
r_text = ""
i = len(text)
while i>0:
r_text = r_text + text[i-1]
i = i-1
return r_text
In [28]:
print reverser("Globalization")
Finally, yet importantly, let read the Zen of Python (python's philosophy).
In [29]:
import this